package rjgc.demo.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iotlwq02.common.utils.PageUtils;
import com.iotlwq02.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import rjgc.demo.Utils.UtilsparsePuPu;
import rjgc.demo.dao.CommodityDao;
import rjgc.demo.entity.CommodityEntity;
import rjgc.demo.service.CommodityService;
/**
 * 
 *
 * @author lwq
 * @email sunlightcs@gmail.com
 * @date 2022-04-27 15:17:35
 */
@RestController
@RequestMapping("xsglxt/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private CommodityDao commodityDao;
    /**
     * name模糊查询列表
     */
    @RequestMapping("/namelist/{name}")
    public R namelist(@PathVariable("name") String name){
        QueryWrapper<CommodityEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        return R.ok().put("pagelist", commodityDao.selectList(wrapper));
    }

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public R list(){
        return R.ok().put("pagelist", commodityService.list());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{name}")
    public R info(@PathVariable("name") String name){
		CommodityEntity commodity = commodityService.getById(name);

        return R.ok().put("commodity", commodity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CommodityEntity commodity){
		commodityService.save(commodity);

        return R.ok();
    }

    /**
     * 实时刷新
     */
    @RequestMapping("/realTimeRefresh")
    public R realTimeRefresh(@RequestBody CommodityEntity commodity) throws IOException, ParseException {
        List<String> list=new ArrayList<String>();
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/b15e1173-1b43-4eb2-8055-b78cb4d3a033");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/d63bfa09-c1e9-4163-856c-ae83b5448f6e");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/63ea3a5a-61dd-4466-ad3f-d656086b8188");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/f7105878-b8a9-4705-bc5c-4868ac46045b");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/1faf5ffd-00cf-4e74-ab8e-aced63ea2554");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/41499400-e0a3-4242-aa45-ecbb17fe4b34");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/8be47b6f-6410-43f5-a665-8531920593e4");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/2c059494-8a76-4218-9ecf-9f26f206fc57");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/96746dc9-10a6-450e-ad86-ec38329ffc3f");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/5890ca2a-4bcb-4bd8-ac7b-9f9601462084");
        list.add("https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/740c377f-7ffc-4e6f-95d6-a5714ea6451d");
        for (String url : list){
            CommodityEntity commodity2=UtilsparsePuPu.parsePuPu(url);
            commodityService.save(commodity2);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CommodityEntity commodity){
		commodityService.updateById(commodity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam("name") String name,@RequestParam("date") String date){
        Map map=new HashMap<String, Object>();
        map.put("name",name);
        map.put("date",date);
		commodityService.removeByMap(map);
        return R.ok();
    }

}
